﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAlipay.Demo
{
    public static class HttpRequestExtensions
    {
        //string displayUrl = HttpContext.Request.GetDisplayUrl();
        //string requestUrl = HttpRequestExtensions.GetRequestUrl(Request);
        //string absoluteUri = HttpRequestExtensions.GetAbsoluteUri(Request);
        //string hostUrl = HttpRequestExtensions.GetHostUri(Request);

        /// <summary>
        /// 获取域名地址  https://localhost:44351
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetHostUri(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host)
                .ToString();
        }

        /// <summary>
        /// 获取相对路径： /pay/index
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetRequestUrl(this HttpRequest request)
        {
            return $"{request.PathBase}{request.Path}{request.QueryString}";
        }

        /// <summary>
        /// 方法1：获取完整/绝对路径    https://localhost:44351/pay/index
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetAbsoluteUri(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host)
                .Append(request.PathBase)
                .Append(request.Path)
                .Append(request.QueryString)
                .ToString();
        }

        ///// <summary>
        ///// 方法2：获取完整/绝对路径,直接使用即可
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //string displayUrl = HttpContext.Request.GetDisplayUrl();

    }
}
